package surf.browser;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

import org.mozilla.browser.impl.components.JImageButton;

import surf.browser.component.TabPane;

/**
 * Stores a TabPane and a reference to the next tab (if there is any).
 * @author Tom Cashman
 *
 */
public class Tab extends JComponent {
	private TabPane m_pane;
	private Tab m_tail;
	private JRadioButton m_close;
	
	/**
	 * Constructor for opening a new tab with no tab following it.
	 * @param pane
	 */
	public Tab(TabPane pane) {
		this(pane, null);
	}
	
	/**
	 * Constructor for opening a new tab with a tab following it.
	 * @param pane
	 * @param tail
	 */
	public Tab(TabPane pane, Tab tail) {
		m_pane = pane;
		m_tail = tail;
		m_close = new JRadioButton("");
		m_close.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				m_pane.getManager().close(m_pane);
			}
		});
		//m_close.setSize(16, 16);
		//m_close.setMaximumSize(new Dimension(26, 24));
		m_close.setVisible(true);
		m_close.setAlignmentY(0.8f);
		
		this.setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
		this.add(m_pane.getLabel());
		this.add(m_close);
		this.addMouseListener(new MouseListener() {
			public void mouseClicked(MouseEvent arg0) {
				m_pane.setVisible(m_pane.isVisible() ? false : true);
				m_pane.setSize(m_pane.getParent().getWidth(), m_pane.getParent().getHeight() - 24);
				m_pane.setButtonText();
				if(m_pane.isVisible()) {
					m_pane.getManager().show(m_pane);
				}
			}

			public void mouseEntered(MouseEvent arg0) {}

			public void mouseExited(MouseEvent arg0) {}

			public void mousePressed(MouseEvent arg0) {}

			public void mouseReleased(MouseEvent arg0) {}
		});
		this.setVisible(true);
	}
	
	/**
	 * Returns the TabPane (the actual browser).
	 * @return
	 */
	public TabPane getTab() {
		return m_pane;
	}
	
	/**
	 * Opens a new tab. This is called recursively until it finds the last tab opened.
	 * Once it does, it attaches a new tab to the end of it.
	 * 
	 * @param m_browser
	 * @return
	 */
	public TabPane openNewTab(WebBrowser m_browser) {
		if(m_tail == null) {
			m_tail = new Tab(new TabPane(m_pane.getManager()));
			m_browser.getContentPane().add(m_tail.getTab());
			//m_pane.setVisible(false);
			return m_tail.getTab();
		} else {
			m_pane.setVisible(false);
			return m_tail.openNewTab(m_browser);
		}
	}
	
	/**
	 * Opens a new tab with a specific url.
	 * @param m_browser
	 * @param url
	 * @return
	 */
	public TabPane openNewTab(WebBrowser m_browser, String url) {
		if(m_tail == null) {
			m_tail = new Tab(new TabPane(m_pane.getManager(), url));
			m_browser.getContentPane().add(m_tail.getTab());
			//m_pane.setVisible(false);
			return m_tail.getTab();
		} else {
			m_pane.setVisible(false);
			return m_tail.openNewTab(m_browser);
		}
	}
	
	/**
	 * Returns the tab following this one, if any.
	 * @return
	 */
	public Tab getTail() {
		return m_tail;
	}
	
	/**
	 * Sets the tab following this one, if any.
	 * @param tail
	 */
	public void setTail(Tab tail) {
		m_tail = tail;
	}
	
	/**
	 * Sets the tab pane (the actual browser) for this tab.
	 * @param pane
	 */
	public void setTabPane(TabPane pane) {
		m_pane = pane;
	}
}
